//
//  SHD_DetaViewController.m
//  B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_DetaViewController.h"

@interface SHD_DetaViewController ()<UIWebViewDelegate>
@property(nonatomic, strong)NSString *night;
@end

@implementation SHD_DetaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _myWebVeiw = [[MyWebView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, (667 - 64) * FitHeight)];
    [self getData];
    _myWebVeiw.delegate = self;
    [_myWebVeiw NightWithType:UIViewColorType2];
    [self.view addSubview:_myWebVeiw];
    
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"nightModel"]) {
        _night = @"1";
    }else{
        _night = @"0";
    }
    
    //注册消息中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNight:) name:@"WebNight" object:nil];
}

-(void)changeNight:(NSNotification *)notification{
    _night = (NSString *)[notification object];
}
-(void)loadView
{
    [super loadView];
    
}
-(void)getData
{
    [HTTPMethod getDataByString:@"http://api2.pianke.me/article/info" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=DE384365-C3F9-468E-8E1A-3E78693E4D79&version=3.0.6", _ID]] WithDataBlock:^(id data) {
        NSString *string = [[data objectForKey:@"data"] objectForKey:@"html"];
        [_myWebVeiw loadHTMLString:string baseURL:nil];
    }];
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if ([_night isEqualToString:@"1"]) {
        [_myWebVeiw stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.backgroundColor= '#555555'"];
        //字体颜色
        [_myWebVeiw stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
    }
    
    //------------------------------------------------------------
    //修改JS语言
    //定义JS字符串(JavaScript)
    NSString *script = [NSString stringWithFormat:@"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var myimg;"
                        "var maxwidth = %f;"//屏幕宽度
                        "for (i = 0; i < document.images.length; i++) {"
                        "myimg = document.images[i];"
                        "myimg.height = maxwidth / (myimg.width / myimg.height);"
                        "myimg.width = maxwidth;"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('p')[0].appendChild(script);", 360 * FitWidth];
    //添加JS
    [webView stringByEvaluatingJavaScriptFromString:script];
    //添加调用JS执行的语句
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    //------------------------------------------------------------
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
